﻿using lingopen.Entity.BLE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lingopen.Foxconn.BLE.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "[{\"IP\":\"192.168.1.11\",\"MAC\":\"50000158FF20\",\"Rssi\":-57,\"EmployeeNo\":\"0004\",\"EmployeeName\":\"李四\",\"EmpLocation\":\"测试AP\",\"DeviceAddrType\":\"0\",\"DeviceName\":\"\",\"Battery\":25,\"Id\":\"798dd443202a453d938f6b77c44da741\",\"IsEnabled\":true,\"CreationTime\":\"2018-11-19T14:36:14\",\"IsDeleted\":false},{\"IP\":\"192.168.1.11\",\"MAC\":\"4000014DFF20\",\"Rssi\":-57,\"EmployeeNo\":\"0003\",\"EmployeeName\":\"王三\",\"EmpLocation\":\"测试AP\",\"DeviceAddrType\":\"0\",\"DeviceName\":\"\",\"Battery\":88,\"Id\":\"bf8fb917835a4778bb11b21ed1042ce5\",\"IsEnabled\":true,\"CreationTime\":\"2018-11-19T14:36:14\",\"IsDeleted\":false},{\"IP\":\"192.168.1.11\",\"MAC\":\"C30687BBD0B4\",\"Rssi\":-66,\"EmployeeNo\":\"0002\",\"EmployeeName\":\"李二\",\"EmpLocation\":\"测试AP\",\"DeviceAddrType\":\"0\",\"DeviceName\":\"Jinou_Beacon\",\"Battery\":2,\"Id\":\"e7c9110d0ad5467fb768b5987fc394c2\",\"IsEnabled\":true,\"CreationTime\":\"2018-11-19T14:36:14\",\"IsDeleted\":false},{\"IP\":\"192.168.1.11\",\"MAC\":\"40000170FF20\",\"Rssi\":-75,\"EmployeeNo\":\"0005\",\"EmployeeName\":\"张五\",\"EmpLocation\":\"测试AP\",\"DeviceAddrType\":\"0\",\"DeviceName\":\"\",\"Battery\":46,\"Id\":\"775045175a53423f9eeebb10752ad0b5\",\"IsEnabled\":true,\"CreationTime\":\"2018-11-19T14:36:13\",\"IsDeleted\":false}]";
            //1.上传记录
            Add_BLE_API(needUpLoads);

        }

        private void Add_BLE_API(List<BLE_Scan_Log> last_list_15)
        {
            try
            {
                foreach (var needAlarm in last_list_15)
                {
                    var insert = new BLE_Transfer_Api()
                    {
                        MAC = needAlarm.MAC,
                        AlarmStatus = "",
                        Battery = needAlarm.Battery,
                        CreationTime = DateTime.Now,// needAlarm.CreationTime,
                        DeviceAddrType = needAlarm.DeviceAddrType,
                        DeviceName = needAlarm.DeviceName,
                        EmpLocation = needAlarm.EmpLocation,
                        EmployeeName = needAlarm.EmployeeName,
                        EmployeeNo = needAlarm.EmployeeNo,
                        IP = needAlarm.IP,
                        Rssi = needAlarm.Rssi,
                        IsDeleted = false,
                        IsEnabled = true,
                        Id = IdHelper.CreateGuid(),
                    };
                    this.DbContext.Insert(insert);
                    string pars = $"EmployeeNo={insert.EmployeeNo}&EmployeeName={insert.EmployeeName}&EmpLocation={insert.EmpLocation}&DateTime={insert.CreationTime.Value.ToString("yyyy-MM-dd HH:mm:ss")}&DeviceAddr={insert.MAC}&DeviceAddrType={insert.DeviceAddrType}&AlarmStatus={insert.AlarmStatus}&DeviceName={insert.DeviceName}&DeviceRssi={insert.Rssi}";
                    var xml = HttpCrossDomain.POST(postUrl, pars);
                    xml = xml.Replace("xmlns=\"http://tempuri.org/\"", "");
                    XmlHelper.XmlDeserialize<string>(xml);


                }
                //删除7天的上传记录 deleteUploadDays 
                this.DbContext.Delete<BLE_Transfer_Api>(a => DateTime.Now.Day == DateTime.Now.AddDays(-1 * deleteUploadDays).Day);//删除前N天的日志文件
            }
            catch (Exception ex)
            {
                localLog.LogTxt($@"LogError", ex.Message);
            }
            //上传 更新 SyncTime
        }
    }
}
